<template>
    <div>
        <div style="display: flex; border: 1px solid #f2f2f2">
            <div class="namebox">
                <span style="color: #ff0000">*</span>
                <span style="white-space: nowrap">客户联系人</span>
            </div>
            <div style="padding: 10px">
                <span style="padding: 10px">选择联系人</span>
                <el-button
                    v-perms="['linkman:add']"
                    size="small"
                    type="primary"
                    @click="handleAdd()"
                >
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增联系人
                </el-button>
                <div style="padding: 0px 10px">
                    <el-checkbox v-model="checked1" label="联系人1" size="large" />
                </div>
                <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists">
                    <el-table-column label="名称" prop="name" min-width="140" />
                    <el-table-column label="工作邮箱" prop="workEmail" min-width="140" />
                    <el-table-column label="私人邮箱" prop="email" min-width="140" />
                    <el-table-column label="称呼" prop="call" min-width="120" />
                    <el-table-column label="性别" prop="sex" min-width="120" />
                    <el-table-column label="联系人类别" prop="type" min-width="150" />
                    <el-table-column label="座机" prop="number" min-width="140" />
                    <el-table-column label="电话" prop="phone" min-width="140" />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['linkman:edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['linkman:del']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <edit-popup
                v-if="showEdit"
                ref="editRef"
                @success="getLists"
                @close="showEdit = false"
            />
        </div>
    </div>
</template>

<script lang="ts" setup>
const checked1 = ref(false)
import { linkmanDelete, linkmanLists } from '@/api/linkman'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './baseEdit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    name: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: linkmanLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await linkmanDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>

<style>
.namebox {
  /* padding: 0px 50px; */
  min-width: 175px;
  background-color: var(--el-bg-color-collapse);
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
